package com.ruoyi.mateproduction.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateproduction.domain.MateReturn;
import com.ruoyi.mateproduction.domain.MateReturnModel;
import com.ruoyi.mateproduction.mapper.MateReturnMapper;
import com.ruoyi.mateproduction.service.MateReturnService;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class MateReturnServiceImpl implements MateReturnService {

    @Autowired
    MateReturnMapper mateReturnMapper;

    @Autowired
    MtAuditMapper mtAuditMapper;

    @Override
    public int insertMateReturn(MateReturn mateReturn) {
        int num = 0;
        try {
            num = mateReturnMapper.insertMateReturn(mateReturn);
            if (mateReturn.getMateReturnModels() != null) {
                for (MateReturnModel mateReturnModel : mateReturn.getMateReturnModels()) {
                    mateReturnModel.setMateReturnId(mateReturn.getMateReturnId());
                    mateReturnModel.setDeptId(SecurityUtils.getDeptId());
                    mateReturnMapper.insertMateReturnModel(mateReturnModel);
                }
            }
        } catch (Exception e) {
            num = 0;
            mateReturnMapper.removeMateReturn(mateReturn.getMateReturnId());
            mateReturnMapper.removeMateReturnModel(mateReturn.getMateReturnId());
        }
        if (num > 0) {
            MtAudit mtAudit = new MtAudit();
            mtAudit.setCompanyId(SecurityUtils.getDeptId());
            mtAudit.setOrderId(mateReturn.getMateReturnId());
            mtAudit.setZjName(mateReturn.getReturnName());
            mtAudit.setOrderType("17");
            mtAudit.setOrderNum(mateReturn.getMateReturnOrderNum());
            mtAudit.setAuditStatus("0");
            mtAudit.setAuditBy(SecurityUtils.getLoginUser().getNickName());
            mtAuditMapper.insertMtAudit(mtAudit);
        }
        return num;
    }

    @Override
    public List<MateReturn> selectMateReturnList(MateReturn mateReturn) {
        return mateReturnMapper.selectMateReturnList(mateReturn);
    }

    @Override
    public MateReturn selectMateReturn(Long mateReturnId) {
        return mateReturnMapper.selectMateReturn(mateReturnId);
    }

    @Override
    public void remove(Long mateReturnId) {
        mateReturnMapper.removeMateReturn(mateReturnId);
        mateReturnMapper.removeMateReturnModel(mateReturnId);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("orderId", mateReturnId);
        resMap.put("orderType", "17");
        mtAuditMapper.deleteMtAuditById(resMap);
    }

    @Override
    public int updateMateReturn(MateReturn mateReturn) {
        int num = 0;
        if (mateReturn.getMateReturnModels() != null) {
            mateReturnMapper.removeMateReturnModel(mateReturn.getMateReturnId());
            for (MateReturnModel mateReturnModel : mateReturn.getMateReturnModels()) {
                mateReturnModel.setDeptId(SecurityUtils.getDeptId());
                mateReturnModel.setMateReturnId(mateReturn.getMateReturnId());
                mateReturnMapper.insertMateReturnModel(mateReturnModel);
            }
            num = mateReturnMapper.updateMateReturn(mateReturn);
        }
        return num;
    }
}
